package com.hulk.sequence;

import com.hulk.sequence.annotation.EnableRedisSeqConfig;
import com.hulk.sequence.builder.RedisSeqBuilder;
import com.hulk.sequence.properties.SequenceRedisProperties;
import com.hulk.sequence.range.impl.name.DateBizName;
import com.hulk.sequence.worker.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * @author hulk
 * @date 2019-05-26
 */


@Configuration
@ConditionalOnMissingBean(Sequence.class)
@ConditionalOnClass({RedisConnectionFactory.class})
@AutoConfigureAfter(RedisAutoConfiguration.class)
@EnableConfigurationProperties(SequenceRedisProperties.class)
@ConditionalOnBean(annotation = EnableRedisSeqConfig.class)
public class RedisSeqAutoConfiguration {

	@Autowired
	private SequenceRedisProperties properties;

//	@Autowired
//	private RedisProperties redisProperties;

	/**
	 * Redis 作为发号器的存储介质
	 *
	 * @param redisProperties
	 * @param properties
	 * @return
	 */
	@Bean
	@ConditionalOnProperty("xsequence.redis")
	public Sequence redisSequence(RedisProperties redisProperties) {
		return RedisSeqBuilder
				.create()
				.bizName(new DateBizName(properties.getBizName()))
				.ip(redisProperties.getHost())
				.port(redisProperties.getPort())
				.auth(redisProperties.getPassword())
				.step(properties.getStep())
				.build();
	}


}